define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'product/index' + location.search,
                    add_url: 'product/add',
                    edit_url: 'product/edit',
                    del_url: 'product/del',
                    multi_url: 'product/multi',
                    import_url: 'product/import',
                    table: 'product',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                clickToSelect: false, //是否启用点击选中
                dblClickToEdit: false, //是否启用双击编辑  
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'fname', title: __('Fname'), operate: 'LIKE'},
                        {field: 'productt.tname', title: '文件分类', operate: 'LIKE'},
                        {field: 'fpath_files', title: __('Fpath_files'), operate: false, formatter: Table.api.formatter.files},
                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'updatetime', title: __('Updatetime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate},
                       {
                        field: 'buttons',
                        width: "120px",
                        title: '按钮组',
                        table: table,
                        events: Table.api.events.operate,
                        buttons: [
                          {
                            name: 'shield',
                            text: '完成',
                            icon: 'fa fa-check-circle fa-fw',
                            classname: 'btn btn-xs btn-success btn-click',
                            click:function(e,s) {       
                              layer.prompt({title:"填写奖金数值"},function(val, index){
                                if(!val || val ==0 ){
                                  layer.msg("请先填写正确的奖金数值！");
                                  return;
                                }
                                if (isNaN(val)) {
                                  layer.msg("请输入数字！");
                                  return;
                                }
                                $.ajax({
                                  type: "post",
                                  url: " ",
                                  data: {
                                    ids:s.id,
                                    bonus:val
                                  },
                                  dataType: "json",
                                  success: function (response) {
                                    if(response.code == 2){
                                      layer.msg(response.msg)
                                    }
                                    else{
                         
                                      layer.close(index);
                                      table.bootstrapTable('refresh');
                                    }
                                  }
                                });
                         
                              });
                              return false
                            },                                
                            refresh: true,
                            visible: function (row) {
                              //返回true时按钮显示,返回false隐藏
                              // console.log(row)
                              return !row.status;
                            }  
                          },
 
                        ],
                        formatter: Table.api.formatter.buttons
                    }

                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
